package com.qf.controller;

import com.qf.entity.User;
import com.qf.entity.custom.ShopCustom;
import com.qf.entity.custom.UserCustom;
import com.qf.service.ShopCustomService;
import com.qf.service.UserService;
import com.qf.tools.JavaWebToken;
import com.qf.tools.ResponseUtils;
import com.qf.tools.ResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description TODO
 * @Author 刘珈玮
 * @Date 2020/7/21 20:03
 */
@RestController
@RequestMapping("/userController")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private ShopCustomService shopCustomService;

    /**
     *
     * 登录接口：以实体类对象形式接收前端传输的数据，解析得到username和password
     *           判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     *           操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param user user实体类接收json字符串
     *
     * @return 返回ResultEntity类型的成功或者失败信息并选择是否需要携带数据
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ResultEntity login(@RequestBody User user) {
        System.out.println(user);
        String username = user.getUsername();
        String password = user.getPassword();
        if (username != null && username != "" && password != null && password != "") {
            user = userService.login(username, password);
            List<ShopCustom> shop_product = shopCustomService.getShop_Product(null);
            if (user != null && shop_product != null) {

                Map<String,Object> m = new HashMap<String,Object>();
                m.put("userId",user.getUserId());
                String javaWebToken = JavaWebToken.createJavaWebToken(m);
                UserCustom userCustom = new UserCustom();
                userCustom.setToken(javaWebToken);
                userCustom.setShopCustomList(shop_product);
                return ResultEntity.success(userCustom);
            } else {
                return ResponseUtils.responseClinet(false);
            }
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }

    /**
     * 注册接口：以实体类对象形式接收前端传输的数据，解析得到username和password
     *           判断是否为空或者空字符串，如果是，则返回失败信息，如果否，则进行service操作
     *           操作成功返回成功信息并返回user实体类数据，操作失败则返回失败信息
     *
     * @param user user实体类接收json字符串
     *
     * @return 返回ResultEntity类型的成功或者失败信息
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public ResultEntity register(@RequestBody User user) {
        String username = user.getUsername();
        String password = user.getPassword();
        if (username != null && username != "" && password != null && password != "") {
            return ResponseUtils.responseClinet(userService.register(username, password));
        } else {
            return ResponseUtils.responseClinet(false);
        }
    }
}
